package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UpdateAccountServlet", value = "/UpdateAccountServlet")
public class UpdateAccountServlet extends HttpServlet {
    private static String MAIN="/WEB-INF/jsp/catalog/Main.jsp";
    private static String UPDATE_ACCOUNT="/WEB-INF/jsp/account/UpdateAccountForm.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Account account=new Account();
        HttpSession session=request.getSession();
        AccountService service=new AccountService();

        account.setUsername(request.getParameter("username"));
        account.setPassword(request.getParameter("password"));
        account.setFirstName(request.getParameter("account.firstname"));
        account.setLastName(request.getParameter("account.lastname"));
        account.setEmail(request.getParameter("account.email"));
        account.setPhone(request.getParameter("account.phone"));
        account.setAddress1(request.getParameter("account.address1"));
        account.setAddress2(request.getParameter("account.address2"));
        account.setCity(request.getParameter("account.city"));
        account.setState(request.getParameter("account.state"));
        account.setZip(request.getParameter("account.zip"));
        account.setCountry(request.getParameter("account.country"));
        System.out.println(account.getStatus()+account.getUsername()+account.getAddress1());

        if(request.getParameter("repeatedPassword").equals(account.getPassword()))
        {
            service.updateAccount(account);
            //System.out.println(account.getPassword());
            request.getRequestDispatcher(MAIN).forward(request,response);
        }
        else
        {
            session.setAttribute("warning","该用户密码前后不一致");
            request.getRequestDispatcher(UPDATE_ACCOUNT).forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request,response);
    }
}
